***************************************************
*****************Paper regressions*****************
***************************************************


**************************************************
***Payment and visits from admin + midline data***
**************************************************

use "$data/midline_clean_merged.dta", clear

eststo clear
foreach var in visited registered paid_union {
eststo: reg `var' program i.stratum, cluster(a7)
sum `var' if program==0
local depvar_mean = `r(mean)'
estadd scalar ControlMean = `depvar_mean'
estadd scalar Observations = `e(N)'
estadd scalar Clusters = `e(N_clust)'
}
use "$data/payment_neighborhood_level_merged.dta", clear
foreach var in compliance revenue_per_person{
eststo: reg `var' program i.stratum
sum `var' if program==0
local depvar_mean = `r(mean)'
estadd scalar ControlMean = `depvar_mean'
estadd scalar Observations = `e(N)'
}
esttab using "$root/outputs/registration_and_payment.txt", scalars(Observations Clusters ControlMean) sfmt(0 0 4 4) nocons l b(3) se(3) r2 compress nogap noobs nonotes nonumbers star(* 0.10 ** 0.05 *** 0.01)  ///
indicate("Stratum FE = *stratum*") replace

*Robustness test: controlling for public lighting

use "$data/midline_clean_merged.dta", clear

foreach var in visited registered paid_union{
reg `var' program avg_light_quality i.stratum, cluster(a7)
}

***************************
****Participation Table****
***************************

use "$data/endline_clean_merged.dta" , clear

global covariates = "age age2 sex bus1 wealth avg_light_quality"
global dependent_variables ="townhall evaluation townhall_or_eval townhall_and_eval participation_index cost_participation_rel_w cost_participation2_rel_w"

eststo clear
foreach depvar in $dependent_variables {
eststo: reg `depvar' program $covariates  i.stratum, cluster(a7)
sum `depvar' if program==0
local depvar_mean = `r(mean)'
estadd scalar ControlMean = `depvar_mean'
estadd scalar Observations = `e(N)'
estadd scalar Clusters = `e(N_clust)'
}
esttab using "$root/outputs/participation_table.txt", scalars(Observations Clusters ControlMean) sfmt(%8.1g) r2 nocons noobs l b(3) se(3) compress nogap nonotes nonumbers star(* 0.10 ** 0.05 *** 0.01)  ///
indicate("Covariates = age age2 sex bus1 wealth avg_light_quality""Stratum FE = *stratum*") nonumbers replace

*RI:

global randomization_inference = 0

if $randomization_inference ==1{

foreach depvar in $dependent_variables {
ritest program _b[program], reps(5000) cluster(a7) strata(stratum) seed(02138): ///
reg `depvar' program $covariates i.stratum, cluster(a7)
}
}


*Multiple test adjustment: Bonferonni p-values
*Following Sankoh et al 1997 / Aker et al 2011

foreach depvar in townhall evaluation{
reg `depvar' program $covariates  i.stratum, cluster(a7)
local p_orig = 2*ttail(e(df_r), abs(_b[program]/_se[program]))
corr townhall evaluation
local corr = `r(rho)'
local g = 2^(1-`corr')
local p_adjust = 1-(1-`p_orig')^`g'
di "`depvar' original p-value:" `p_orig'
di "`depvar' adjusted p-value:" `p_adjust'
}

*********************
****Decay analysis***
*********************

use "$data/endline_clean_merged.dta" , clear
global covariates = "age age2 sex bus1 wealth avg_light_quality"

*Time graphs

reg participation_index program $covariates i.stratum, cluster(a7)
g coef0 = _b[program]
g ub0 = _b[program]+1.96*abs(_se[program])
g lb0 = _b[program]-1.96*abs(_se[program])
g time_min0 = 0
g time_max0 = 0


*5-15 quantiles

forv y = 5(5)15{

preserve

forv x = 1/`y'{
reg participation_index program $covariates i.stratum if time_xtile`y'==`x', cluster(a7)
g coef`x' = _b[program]
g ub`x' = _b[program]+1.96*abs(_se[program])
g lb`x' = _b[program]-1.96*abs(_se[program])
sum time_lag if time_xtile`y'==`x'
g time_min`x' = `r(min)'
g time_max`x' = `r(max)'
}

keep coef* ub* lb* time_min* time_max*
keep if _n==1

local groups = `y'+1
stack _all, group(`groups') clear

replace _stack = _stack-1


sum coef if _stack==0
local avg_effect = `r(mean)'
sum lb if _stack==0
local avg_lb = `r(mean)'
sum ub if _stack==0
local avg_ub = `r(mean)'

forv x=1/`y'{
sum time_min0 if _stack==`x'
local min = `r(mean)'
sum time_max0 if _stack==`x'
local max = `r(mean)'
local time`x' = "`min'-`max'"
}


if `y'==5{
twoway (lowess coef _stack if _stack>0) (scatter coef _stack if _stack>0, ///
xtitle("Time gap quantile") ysc(range(-.1/.3)) ///
ylab(-.1 "-0.1" 0 "0" 0.1 "0.1" 0.2 "0.2" .3 "0.3") xscale(range(1/`y')) ///
yline(`avg_effect', lcolor(navy)) ///
yline(`avg_ub', lcolor(emidblue)) ///
yline(`avg_lb', lcolor(emidblue)) ///
legend(label(1 "Lowess line fit of treatment effects") label(2 "Scatterplot of treatment effects") ///
label(3 "Average treatment effect and 95% CI") ring(0) position(1)) ///
xlabel(1 "`time1' Days" 2 "`time2' Days" 3 "`time3' Days" 4 "`time4' Days" 5 "`time5' Days", angle(45)))
graph export "$root/outputs/coefplot_`y'quantiles_timegap.pdf", replace
}

if `y'==10{
twoway (lowess coef _stack if _stack>0) (scatter coef _stack if _stack>0, ///
xtitle("Time gap quantile") xscale(range(1/`y')) ///
yline(`avg_effect', lcolor(navy)) ///
yline(`avg_ub', lcolor(emidblue)) ///
yline(`avg_lb', lcolor(emidblue)) ///
legend(label(1 "Lowess line fit of treatment effects") label(2 "Scatterplot of treatment effects") ///
label(3 "Average treatment effect and 95% CI") ring(0) position(11)) ///
xlabel(1 "`time1' Days" 2 "`time2' Days" 3 "`time3' Days" 4 "`time4' Days" 5 "`time5' Days" ///
6 "`time6' Days" 7 "`time7' Days" 8 "`time8' Days" 9 "`time9' Days" 10 "`time10' Days", angle(45)))
graph export "$root/outputs/coefplot_`y'quantiles_timegap.pdf", replace
}
if `y'==15{
twoway (lowess coef _stack if _stack>0) (scatter coef _stack if _stack>0, ///
xtitle("Time gap quantile") xscale(range(1/`y')) ///
yline(`avg_effect', lcolor(navy)) ///
yline(`avg_ub', lcolor(emidblue)) ///
yline(`avg_lb', lcolor(emidblue)) ///
legend(label(1 "Lowess line fit of treatment effects") label(2 "Scatterplot of treatment effects") ///
label(3 "Average treatment effect and 95% CI") ring(0) position(11)) ///
xlabel(1 "`time1' Days" 2 "`time2' Days" 3 "`time3' Days" 4 "`time4' Days" 5 "`time5' Days" ///
6 "`time6' Days" 7 "`time7' Days" 8 "`time8' Days" 9 "`time9' Days" 10 "`time10' Days" ///
11 "`time11' Days" 12 "`time12' Days" 13 "`time13' Days" 14 "`time14' Days" 15 "`time15' Days", angle(45)))
graph export "$root/outputs/coefplot_`y'quantiles_timegap.pdf", replace
}

restore
}


*20 quantiles - special graph with outlier

forv x = 1/20{
reg participation_index program $covariates i.stratum  if time_xtile20==`x', cluster(a7)
g coef`x' = _b[program]
g ub`x' = _b[program]+1.96*abs(_se[program])
g lb`x' = _b[program]-1.96*abs(_se[program])
sum time_lag if time_xtile20==`x'
g time_min`x' = `r(min)'
g time_max`x' = `r(max)'
}

keep coef* ub* lb* time_min* time_max*
keep if _n==1

stack _all, group(21) clear

replace _stack = _stack-1


sum coef if _stack==0
local avg_effect = `r(mean)'
sum lb if _stack==0
local avg_lb = `r(mean)'
sum ub if _stack==0
local avg_ub = `r(mean)'

forv x=1/20{
sum time_min0 if _stack==`x'
local min = `r(mean)'
sum time_max0 if _stack==`x'
local max = `r(mean)'
local time`x' = "`min'-`max'"
}

g gap = time_max0-time_min0

twoway (lowess coef _stack if _stack>1) (scatter coef _stack if _stack>1, ///
xtitle("Time gap quantile") xscale(range(2/20)) ///
yline(`avg_effect', lcolor(navy)) ///
yline(`avg_ub', lcolor(emidblue)) ///
yline(`avg_lb', lcolor(emidblue)) ///
legend(label(1 "Lowess line fit of treatment effects") label(2 "Scatterplot of treatment effects") ///
label(3 "Average treatment effect and 95% CI") ring(0) position(11)) ///
xlabel(1 "`time1' Days" 2 "`time2' Days" 3 "`time3' Days" 4 "`time4' Days" 5 "`time5' Days" ///
6 "`time6' Days" 7 "`time7' Days" 8 "`time8' Days" 9 "`time9' Days" 10 "`time10' Days" ///
11 "`time11' Days" 12 "`time12' Days" 13 "`time13' Days" 14 "`time14' Days" 15 "`time15' Days"  ///
16 "`time16' Days" 17 "`time17' Days" 18 "`time18' Days" 19 "`time19' Days" 20 "`time20' Days" , angle(45)))
graph export "$root/outputs/coefplot_20quantiles_timegap.pdf", replace


********************************
****Beliefs about government****
********************************

use "$data/endline_clean_merged.dta" , clear


global covariates = "age age2 sex bus1 wealth avg_light_quality"
global dependent_variables = "gov_provide capacity_info capacity_punish others4 eval_trust_tax capacity_provide eval_trust_gov funds_deposited  funds_spent transparency "

eststo clear
foreach depvar in $dependent_variables  {
local counter = `counter' + 1
reg `depvar' program $covariates i.stratum , cluster(a7)
local beta = round(_b[program],.001)
di "Beta: `beta'"
local se = round(_se[program],.001)
di "SE: `se'"
local p = round(2*ttail(e(df_r), abs(_b[program]/_se[program])),.001)
di "p-value: `p'"
local obs = round(`e(N)',1)
di "N:`obs'"
local clust = round(`e(N_clust)',1)
di "Clusters:`clust'"
local r2 = `e(r2)' 
di "R2:`r2'"
sum `depvar' if program==0
local controlmean = `r(mean)'
di "Control mean:`controlmean'"

	if `counter' == 1 { 
		mat input reg = (`beta', `se',`r2',`obs',`controlmean') 
		mat rownames reg = `depvar' 
		mat colnames reg = beta SE r2 N controlmean	 
	}
	

	if `counter' != 1 { 
		mat input reg`counter' = (`beta', `se',`r2',`obs',`controlmean') 
		mat rownames reg`counter' = `depvar' 
		mat colnames reg`counter' = beta SE r2 N controlmean	 
		mat reg = (reg \ reg`counter' )
	}

}
	
	mata reg = st_matrix("reg") 
	mat list reg 	
	
	mmat2tex reg using "$root/outputs/belief_table_condensed.txt", replace  ///
	colnames(beta SE r2 N controlmean) ///
	rownames("Resp. for public goods provision" "Information about taxpayers"  "Ability to punish tax evaders" "Perceived citizen tax compliance" ///
	"Performance of tax ministry"  ///
	"Ability to provide public goods" "Performance of government" "Integrity of tax collectors" "Integrity of government spending" "Transparency of government") ///
	preheader("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{5}{c}} \hline\hline") ///
	bottom("\hline\hline \end{tabular} }") ///
	fmt(%9.3f)



************************
***COMPLIER ANALYSIS****
************************

use "$data/endline_clean_merged.dta" , clear


global covariates = "age age2 sex bus1 wealth avg_light_quality" 
global dependent_variables = "townhall_or_eval"
global instruments = "paid_jive_u_avg registered_jive_u_avg"

foreach depvar in $dependent_variables{
eststo clear
eststo: reg `depvar' registered $covariates i.stratum if  program==1, cluster(a7)
sum `depvar' if program==1
local depvar_mean = `r(mean)'
estadd scalar DepVarMean = `depvar_mean'
estadd scalar Observations = `e(N)'
estadd scalar Clusters = `e(N_clust)'
eststo: reg `depvar' registered $covariates i.stratum i.a1 if  program==1, cluster(a7)
sum `depvar' if  program==1
local depvar_mean = `r(mean)'
estadd scalar DepVarMean = `depvar_mean'
estadd scalar Observations = `e(N)'
estadd scalar Clusters = `e(N_clust)'
eststo: xi: reg `depvar' registered $covariates i.stratum i.col1_union  if  program==1, cluster(a7)
sum `depvar' if  program==1
local depvar_mean = `r(mean)'
estadd scalar DepVarMean = `depvar_mean'
estadd scalar Observations = `e(N)'
estadd scalar Clusters = `e(N_clust)'
eststo: xi: ivreg2 `depvar' (registered = registered_jive_u_avg double_bonus) $covariates i.stratum i.a1  if program==1, cluster(a7) first 
estadd scalar Observations = `e(N)'
estadd scalar Clusters = `e(N_clust)'
mat first1=e(first)
scalar apf_1a=first1[3,1]
estadd scalar Fstat=scalar(apf_1a)
sum `depvar' if program==1
local depvar_mean = `r(mean)'
estadd scalar DepVarMean = `depvar_mean'
eststo: reg `depvar' paid_receipt_union registered $covariates i.stratum if  program==1, cluster(a7)
sum `depvar' if  program==1
local depvar_mean = `r(mean)'
estadd scalar DepVarMean = `depvar_mean'
estadd scalar Observations = `e(N)'
estadd scalar Clusters = `e(N_clust)'
eststo: xi: ivreg2 `depvar' (registered paid_receipt_union = $instruments double_bonus) $covariates i.stratum i.a1 if program==1, cluster(a7) first 
estadd scalar Observations = `e(N)'
estadd scalar Clusters = `e(N_clust)'
mat first1=e(first)
scalar apf_1a=first1[7,1]
estadd scalar AP_F1=scalar(apf_1a)
scalar apf_1b=first1[7,2]
estadd scalar AP_F2=scalar(apf_1b)
sum `depvar' if program==1
local depvar_mean = `r(mean)'
estadd scalar DepVarMean = `depvar_mean'
eststo: reg `depvar' paid_receipt_union registered col_time_spent_log observed_tablet num_cols visits $covariates i.stratum if  program==1, cluster(a7)
sum `depvar' if  program==1
local depvar_mean = `r(mean)'
estadd scalar DepVarMean = `depvar_mean'
estadd scalar Observations = `e(N)'
estadd scalar Clusters = `e(N_clust)'
eststo: reg `depvar' paid_receipt_union registered col_time_spent_log observed_tablet num_cols visits $covariates i.stratum i.a1 if  program==1, cluster(a7)
sum `depvar' if  program==1
local depvar_mean = `r(mean)'
estadd scalar DepVarMean = `depvar_mean'
estadd scalar Observations = `e(N)'
estadd scalar Clusters = `e(N_clust)'
}
esttab using "$root/outputs/complier_margins.txt", ///
scalars(Observations Clusters DepVarMean) sfmt(0 0 3 3) ///
nocons r2 noobs l b(3) se(3) compress nogap nonotes ///
star(* 0.10 ** 0.05 *** 0.01) nonumbers replace ///
indicate("Covariates = age age2 sex bus1 wealth avg_light_quality" ///
"Stratum FE = *stratum*""Enumerator FE = *a1*""Collector FE = *col1_unio*")


**********************************************
***Townhall and evaluation comment analysis***
**********************************************


use "$data/townhall_comments.dta", clear

hist comment , percent xlabel(1/9,valuelabel angle(55) labsize(small) labcolor(black)) yscale(range(0 35)) ylab(0(5)35) ///
 xtitle("") bfcolor(midblue) barwidth(.8) bcolor(black) discrete ytitle( , size(medium)) xscale(extend)
graph export "$root/outputs/townhall_comments.pdf", replace

*LDA Topic Analysis

replace note = "" if note=="0"
keep if note!=""

replace note = subinstr(note,".","",.)
replace note = subinstr(note,",","",.)
replace note = subinstr(note,"!","",.)
replace note = subinstr(note,"?","",.)
replace note = subinstr(note,":","",.)
replace note = subinstr(note,"payent","payer",.)
replace note = subinstr(note,"paiement","payer",.)
replace note = subinstr(note,"paie","payer",.)
replace note = subinstr(note,"payé","payer",.)
replace note = subinstr(note,"connaissent","sait",.)
replace note = subinstr(note,"connaissons","sait",.)
replace note = subinstr(note,"impôts","impôt",.)
replace note = subinstr(note,"impot","impôt",.)
replace note = subinstr(note,"l'impôt","impôt",.)
replace note = subinstr(note,"pourmque","pour que",.)
replace note = subinstr(note,"font","faire",.)

ldagibbs note, name_new_var("topic_probability") seed(48761) topics(5) normalize mat_save path("$root/outputs") ///
stopwords("nous je il elle on le la les a ce ne pas de que qui est celle chez aux des ils à et si sa au 2ou 3 lui en ya ont non ni n'y 2 !!!! vos an ces ceux son 5 ou votre vous sur notre n'est comment ... dans car aussi mais , pour c'est n'en ...? se qu'il même été sont n'ya mon du ceci comme êtes par") // Remove all instances of these French stopwords

wprobimport using "$root/outputs/word_prob"


count 
local tot_obs_plus = `r(N)' + 1

forv x = 1/5{
di in red "Topic `x'"
sort word_prob`x', stable
g topic`x'= words
g rank`x' = `tot_obs_plus'-_n
replace topic`x'="" if rank`x'>10
preserve
keep topic`x' rank`x'
drop if topic`x'==""
rename rank`x' rank
tempfile topic`x'
sa `topic`x''
restore
}

use `topic1', clear
forv x= 2/5{
merge 1:1 rank using `topic`x'', nogen
}

order rank topic*

forv x = 1/5 {
g topic`x'_eng=topic`x'
replace topic`x'_eng="necessary" if topic`x'=="faut"
replace topic`x'_eng="collectors" if topic`x'=="percepteurs"
replace topic`x'_eng="pay" if topic`x'=="payer"
replace topic`x'_eng="information campaign" if topic`x'=="sensibilisation"
replace topic`x'_eng="nothing" if topic`x'=="rien"
replace topic`x'_eng="payment" if topic`x'=="paiement"
replace topic`x'_eng="everyone" if topic`x'=="tout"
replace topic`x'_eng="all" if topic`x'=="tous"
replace topic`x'_eng="introduce" if topic`x'=="introduire"
replace topic`x'_eng="know" if topic`x'=="sait"
replace topic`x'_eng="why" if topic`x'=="pourquoi"
replace topic`x'_eng="others" if topic`x'=="autres"
replace topic`x'_eng="must" if topic`x'=="doit"
replace topic`x'_eng="tax" if topic`x'=="impôt"
replace topic`x'_eng="information" if topic`x'=="l'information"
replace topic`x'_eng="decision" if topic`x'=="décision"
replace topic`x'_eng="include" if topic`x'=="partis"
replace topic`x'_eng="without" if topic`x'=="sans"
replace topic`x'_eng="take" if topic`x'=="prenez"
replace topic`x'_eng="competence" if topic`x'=="compétence"
replace topic`x'_eng="agreement" if topic`x'=="accord"
replace topic`x'_eng="discipline" if topic`x'=="discipliner"
replace topic`x'_eng="benefit" if topic`x'=="bénéficier"
replace topic`x'_eng="profits" if topic`x'=="réalisations"
replace topic`x'_eng="before" if topic`x'=="avant"
replace topic`x'_eng="time" if topic`x'=="fois"
replace topic`x'_eng="different" if topic`x'=="differentes"
replace topic`x'_eng="categories" if topic`x'=="catégories"
replace topic`x'_eng="management" if topic`x'=="gestion"
replace topic`x'_eng="palpable" if topic`x'=="palpables"
replace topic`x'_eng="politicians" if topic`x'=="députés"
replace topic`x'_eng="change" if topic`x'=="changent"
replace topic`x'_eng="information campaign" if topic`x'=="vulgarisation"
replace topic`x'_eng="do" if topic`x'=="faire"
replace topic`x'_eng="me" if topic`x'=="moi"
replace topic`x'_eng="monitoring" if topic`x'=="suivi"
replace topic`x'_eng="exempt" if topic`x'=="exempté"
replace topic`x'_eng="inform" if topic`x'=="informer"
replace topic`x'_eng="sufficient" if topic`x'=="suffisante"
replace topic`x'_eng="regret" if topic`x'=="regrette"
replace topic`x'_eng="amount" if topic`x'=="montant"
replace topic`x'_eng="time" if topic`x'=="temps"
replace topic`x'_eng="transparency" if topic`x'=="transparence"
replace topic`x'_eng="circulate" if topic`x'=="passent"
replace topic`x'_eng="tax" if topic`x'=="impôt"
replace topic`x'_eng="schools" if topic`x'=="écoles"
replace topic`x'_eng="see" if topic`x'=="vois"
}

drop topic1-topic5
order rank topic*
rename rank Order
rename topic*_eng Topic*
dataout, save("$root/outputs/townhall_topics.txt") tex replace noauto

** Evaluation comment analysis **

use "$data/endline_clean_merged.dta" , clear

sort eval_order

keep eval_comment_en
keep if eval_comment_en!=""

replace eval_comment_en = subinstr(eval_comment_en,".","",.)
replace eval_comment_en = subinstr(eval_comment_en,",","",.)
replace eval_comment_en = subinstr(eval_comment_en,"!","",.)
replace eval_comment_en = subinstr(eval_comment_en,"?","",.)
replace eval_comment_en = subinstr(eval_comment_en,":","",.)

forv x=0/9{
replace eval_comment_en = subinstr(eval_comment_en,"`x'","",.)
}


ldagibbs eval_comment_en, name_new_var("topic_probability") seed(3) topics(5) normalize mat_save path("$root/outputs") ///
stopwords("I you his her your the of and in to it with has its we don't on but an for which all place as if like also them us at a no this there our are here from have by they will their not that be is") 


wprobimport using "$root/outputs/word_prob"

count 
local tot_obs_plus = `r(N)' + 1


forv x = 1/5{
di in red "Topic `x'"
sort word_prob`x', stable
g topic`x'= words
g rank`x' = `tot_obs_plus'-_n
replace topic`x'="" if rank`x'>10
preserve
keep topic`x' rank`x'
drop if topic`x'==""
rename rank`x' rank
tempfile topic`x'
sa `topic`x''
restore
}

use `topic1', clear
forv x= 2/5{
merge 1:1 rank using `topic`x'', nogen
}

order rank topic*
rename rank Order
rename topic* Topic*

dataout, save("$root/outputs/eval_comment_topics.txt") tex replace noauto


** Graph of evaluation comments **

use "$data/endline_clean_merged.dta" , clear

drop if eval_comment_topic1==. & eval_comment_topic2==.
keep  eval_comment_topic1 eval_comment_topic2 eval_comment_topic3 
stack  eval_comment_topic1 eval_comment_topic2 eval_comment_topic3, into(comments) clear

label define comments 1 `"Better governance (general)"', modify
label define comments 2 `"Better public goods provision"', modify
label define comments 3 `"More monitoring of government"', modify
label define comments 4 `"Better use of taxes/revenues"', modify
label define comments 5 `"More citizen voice in politics"', modify
label define comments 6 `"More security / peace"', modify
label define comments 7 `"Complimentary of government"', modify
label define comments 8 `"Question on NGOs/Researchers"', modify
label define comments 9 `"Other"', modify
label value comments comments

hist comments , percent xlabel(1/9,valuelabel angle(55) labsize(small) labcolor(black)) ytitle(,size(medlarge)) yscale(range(0 40)) ylab(0(10)40) ///
xtitle("") barwidth(.8) bcolor(black) bfcolor(midblue) discrete saving(`var', replace)
graph export "$root/outputs/eval_comments.pdf", replace

*****************************
*****************************
*****************************
******  BALANCE TABLE  ******
*****************************
*****************************
*****************************

use "$data/endline_clean_merged.dta" , clear

******************************************
****Individual-level endline variables****
******************************************

global vars1 = "age female no_schooling edu_yrs_orig inc_dollars wealth_n unemployed bus1 work_gov property1 born_kga luluwa native_speaker any_renters any_electricity house1 vehicle "

local counter = 0 
foreach var of varlist $vars1 {
local counter = `counter' + 1

reg `var' program i.stratum, cluster(a7)

local beta = round(_b[program],.0001)
local se = round(_se[program],.0001)
local p = round(2*ttail(e(df_r), abs(_b[program]/_se[program])),.0001)
di in red "p-value: `p'"
sum `var',d


	if `counter' == 1 { 
		mat input bal1 = (`r(mean)',`r(sd)',`r(min)',`r(max)',`r(N)', `beta', `se') 
		mat rownames bal1 = `var' 
		mat colnames bal1 = Mean SD Min Max N Diff SE 	 
		di "hello"
	}
	

	if `counter' != 1 { 
		mat input bal1_`counter' = (`r(mean)',`r(sd)',`r(min)',`r(max)',`r(N)', `beta', `se') 
		mat rownames bal1_`counter' = `var' 
		mat colnames bal1 = Mean SD Min Max N Diff SE 	 
		mat bal1 = (bal1 \ bal1_`counter' )
	}
}
	
	
	mata bal1 = st_matrix("bal1") 
	mat list bal1 	
	
	
*Omnibus test of joint orthogonality:

reg program $vars1 i.stratum, cluster(a7)
test $vars1
	

**********************************************
********Neighborhood-level variables**********
**********************************************

use  "$data/neighborhood_covariates.dta", clear
drop if a7>=400 & a7<500 // Nganza commune dropped b/c of violence

global vars2 = "avg_road_quality avg_light_quality polyarea pop_density hh_size hh_quality expenditure_weekly knows_dgrkoc past_collector_visit past_payment political_participation gov_trust gov_eval govimpprovide gov_corrupt trust_researchers"

local counter = 0 
foreach var of varlist $vars2 {
local counter = `counter' + 1

reg `var' program i.stratum, r

local beta = round(_b[program],.0001)
local se = round(_se[program],.0001)
local p = round(2*ttail(e(df_r), abs(_b[program]/_se[program])),.0001)
di in red "p-value: `p'"
sum `var',d

	if `counter' == 1 { 
		mat input bal2 = (`r(mean)',`r(sd)',`r(min)',`r(max)',`r(N)', `beta', `se') 
		mat rownames bal2 = `var' 
		mat colnames bal2 = Mean SD Min Max N Diff SE 	 
	}
	

	if `counter' != 1 { 
		mat input bal2_`counter' = (`r(mean)',`r(sd)',`r(min)',`r(max)',`r(N)', `beta', `se') 
		mat rownames bal2_`counter' = `var' 
		mat colnames bal2 = Mean SD Min Max N Diff SE 	 
		mat bal2 = (bal2 \ bal2_`counter' )
	}
}
	
	mata bal2 = st_matrix("bal2") 
	mat list bal2 
	

*Omnibus test of joint orthogonality:

reg program $vars2 i.stratum, cluster(a7)
test $vars2

******************************************
***Other aspects of experimental design***
******************************************

*Variables from endline survey

use "$data/endline_survey_administration.dta", clear
merge m:1 a7 using "$data/treatment_assignment.dta", keep(1 3) nogen

global vars3 = "s3_refusal attritor"

local counter = 0 
foreach var of varlist $vars3 {
local counter = `counter' + 1

reg `var' program i.stratum, cluster(a7)

local beta = round(_b[program],.0001)
local se = round(_se[program],.0001)
local p = round(2*ttail(e(df_r), abs(_b[program]/_se[program])),.0001)
di in red "p-value: `p'"
sum `var', d

	if `counter' == 1 { 
		mat input bal3 = (`r(mean)',`r(sd)',`r(min)',`r(max)',`r(N)', `beta', `se') 
		mat rownames bal3 = `var' 
		mat colnames bal3 = Mean SD Min Max N Diff SE 	 
	}
	

	if `counter' != 1 { 
		mat input bal3_`counter' = (`r(mean)',`r(sd)',`r(min)',`r(max)',`r(N)', `beta', `se') 
		mat rownames bal3_`counter' = `var' 
		mat colnames bal3 = Mean SD Min Max N Diff SE 	 
		mat bal3 = (bal3 \ bal3_`counter' )
	}
}
	
	mata bal3 = st_matrix("bal3") 
	mat list bal3 

	

*Variables from baseline survey
	
use  "$data/baseline_clean.dta", clear
merge m:1 a7 using "$data/treatment_assignment.dta", keep(1 3) nogen

gen s1_refusal = y9 !=. & photo=="" // Survey interrupted in the middle

global vars4 = "s1_refusal "

local counter = 0 
foreach var of varlist $vars4 {
local counter = `counter' + 1

reg `var' program i.stratum, cluster(a7)

local beta = round(_b[program],.0001)
local se = round(_se[program],.0001)
local p = round(2*ttail(e(df_r), abs(_b[program]/_se[program])),.0001)
di in red "p-value: `p'"
sum `var', d

	if `counter' == 1 { 
		mat input bal4 = (`r(mean)',`r(sd)',`r(min)',`r(max)',`r(N)', `beta', `se') 
		mat rownames bal4 = `var' 
		mat colnames bal4 = Mean SD Min Max N Diff SE 	 
	}
	

	if `counter' != 1 { 
		mat input bal4_`counter' = (`r(mean)',`r(sd)',`r(min)',`r(max)',`r(N)', `beta', `se') 
		mat rownames bal4_`counter' = `var' 
		mat colnames bal4 = Mean SD Min Max N Diff SE 	 
		mat bal4 = (bal4 \ bal4_`counter' )
	}
}
	
	mata bal4 = st_matrix("bal4") 
	mat list bal4 


*Variables from midline survey


use "$data/midline_clean_merged.dta", clear

g s2_refusal = respondent==0

global vars5 = "s2_refusal"

local counter = 0 
foreach var of varlist $vars5 {
local counter = `counter' + 1

reg `var' program i.stratum, cluster(a7)

local beta = round(_b[program],.0001)
local se = round(_se[program],.0001)
local p = round(2*ttail(e(df_r), abs(_b[program]/_se[program])),.0001)
di in red "p-value: `p'"
sum `var', d

	if `counter' == 1 { 
		mat input bal5 = (`r(mean)',`r(sd)',`r(min)',`r(max)',`r(N)', `beta', `se') 
		mat rownames bal5 = `var' 
		mat colnames bal5 = Mean SD Min Max N Diff SE 	 
	}
	

	if `counter' != 1 { 
		mat input bal5_`counter' = (`r(mean)',`r(sd)',`r(min)',`r(max)',`r(N)', `beta', `se') 
		mat rownames bal5_`counter' = `var' 
		mat colnames bal5 = Mean SD Min Max N Diff SE 	 
		mat bal5 = (bal5 \ bal5_`counter' )
	}
}
	
	mata bal5 = st_matrix("bal5") 
	mat list bal5 


* Nganza commune dropped b/c of violence
use "$data/treatment_assignment.dta", clear

g neighborhoods_dropped = nganza==1| non_residential==1

global vars6 = "neighborhoods_dropped"

local counter = 0 
foreach var of varlist $vars6 {
local counter = `counter' + 1

reg `var' program i.stratum, r

local beta = round(_b[program],.0001)
local se = round(_se[program],.0001)
local p = round(2*ttail(e(df_r), abs(_b[program]/_se[program])),.0001)
di in red "p-value: `p'"
sum `var', d

	if `counter' == 1 { 
		mat input bal6 = (`r(mean)',`r(sd)',`r(min)',`r(max)',`r(N)', `beta', `se') 
		mat rownames bal6 = `var' 
		mat colnames bal6 = Mean SD Min Max N Diff SE 	 
	}
	

	if `counter' != 1 { 
		mat input bal6_`counter' = (`r(mean)',`r(sd)',`r(min)',`r(max)',`r(N)', `beta', `se') 
		mat rownames bal6_`counter' = `var' 
		mat colnames bal6 = Mean SD Min Max N Diff SE 	 
		mat bal6 = (bal6 \ bal6_`counter' )
	}
}
	
	mata bal6 = st_matrix("bal6") 
	mat list bal6 

	
*Combine everything	

	mat balance = (bal2 \ bal1)
	mat balance = (balance \ bal6)
	mat balance = (balance \ bal4)
	mat balance = (balance \ bal5)
	mat balance = (balance \ bal3)
	mata balance = st_matrix("balance") 
	mat list balance 
	

	mmat2tex balance using "$root/outputs/full_balance_table.txt", replace  ///
	rownames($vars2 $vars1 $vars6 $vars4 $vars5 $vars3) ///
	colnames(Mean SD Min Max N Diff SE 	 ) ///
	preheader("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{tabular}{l*{7}{c}} \hline\hline") ///
	substitute(age "Age (years)" female "Female (dummy)" inc_dollars "Monthly income (USD)" wealth_n "Household wealth index (normalized)" ///
	bus1 "Owns business of any size (dummy)" work_gov "Government worker (dummy)" property1 "Multiple plot owner  (dummy)" ///
	born_kga "Born in Kananga (dummy)" luluwa "Majority ethnicity (dummy)" native_speaker "Native Tshiluba speaker (dummy)" any_renters "Has renters in compound (dummy)" ///
	house1 "House with non-mudbrick walls (dummy)" vehicle "Owns vehicle (dummy)" polyarea "Neighborhood size (square km)" ///
	pop_density "Population density (pop./square km)" avg_road_quality "Quality of roads (normalized)" ///
	avg_light_quality "Quality of public lighting (normalized)" unemployed "Unemployed (dummy)" education "Education (years)"  ///
	hh_size "Household size (number of adults)" no_schooling "No Schooling (dummy)" ///
	expenditure_weekly "Weekly expenditure (normalized)" trust_researchers "Trust in researchers (normalized)" ///
	hh_quality "House quality index (normalized)"  ///
	past_collector_visit "Reports any prior collector visit (dummy)" past_payment "Reports ever paying property tax (dummy)" ///
	knows_dgrkoc "Knows provincial tax ministry (dummy)" political_participation "Political participation index (normalized)" ///
	gov_eval "Performance of government (normalized)" gov_corrupt "Integrity of government spending (normalized)" ///
	gov_trust "Trust in government (normalized)" govimpprovide "Resp. for public goods provision (normalized)"  /// 
	neighborhoods_dropped "Neighborhoods without endline (dummy)" edu_yrs_orig "Education (years)" any_electricity "Any source of electricity (dummy)"  ///
	s1_refusal "Refusals in baseline survey (dummy)" s2_refusal "Refusals midline survey (dummy)" s3_refusal "Refusals in endline survey (dummy)" attritor "Any attrition in endline survey (dummy)")  ///
	bottom("\hline\hline \end{tabular} }")	///
	fmt(%12.2f %12.2f %12.0f %12.0f %12.0f %12.3f)
	


